<?php
class Admin_Categories extends Admin_Controller{
	
	public $namespace = 'news';
	public $section = 'categories';
	function __construct(){
		parent::__construct();
		$this->load->model('categories_m');
		$this->check_permissions();
	}
	
	function index(){	
		$data =  $this->categories_m->get_all();
		$model = array();
		//chuyen de quy thanh array cap 1
		array_level1($data,$model);
		$m = array();
		foreach($model as $item){
			if(count($item) > 0){
				$m[] = $item;
			}
		}
//		print_r($model);exit;
//		print_r($model);exit;
		$this->template
			->set('model',$m)
			->set('action',$this->action)
			->build($this->controller.'/'.$this->method);
	}
	function delete($id){
		$this->categories_m->delete($id);
		redirect('admin/news/categories');
	}
	
	function ordering(){
		$model = $this->input->post();
		$id = $model['id'];
		unset($model['id']);
		$this->categories_m->ordering($id,$model);
	}
	
	function create(){
		$model = $this->input->post();
		
		//validate
		$this->form_validation->set_rules('title', lang('global:title'), 'required');
		$this->form_validation->set_rules('slug', lang('global:slug'), 'required');
		
		$type =  substr($this->input->post('btnAction'), 0);
		
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); // bo những biến post wa ko phải dữ liệu
					}
				}
				
				$id_insert = $this->categories_m->insert($model);
				if($type == 'save')
					redirect('admin/'.$this->namespace.'/'.$this->section);
				if($type == 'add')
					redirect('admin/'.$this->namespace.'/'.$this->section.'/'.$this->method);
				if($type == 'edit')
					redirect('admin/'.$this->namespace.'/'.$this->section.'/edit/'.$id_insert);		
			}
		}
		//categories 
		$categories_radio = $this->categories_m->list_categories_radio(NULL,$name='parent_id', 0, NULL );//de quy

	 	
		$this->template
			->set('model',$model)
			->set('action',$this->action)
			->set('categories_radio',$categories_radio)
			->set_breadcrumb(lang('news:categories'), 'admin/news/categories')
			->set_breadcrumb(lang('news:cat:add'))
			->build($this->controller.'/form');
	}
	
	function edit( $id ){
		$model = (array)$this->categories_m->get( $id );
		if(!$model)
			redirect(404);
		$this->form_validation->set_rules('title', lang('global:title'), 'required');
		$this->form_validation->set_rules('slug', lang('global:slug'), 'required');
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$model = $this->input->post();
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); // bo những biến post wa ko phải dữ liệu
					}
				}
				
				//UPLOAD IMAGE
				$this->config->load('upload');
				$config = $this->config->item('image');
				$config['upload_path'] = $this->path_upload;
				$this->load->library('upload',$config);
				
				if($this->upload->do_upload('upload')){
					$image = $this->upload->data();
					//tao THUMB
					$this->load->library('image_lib');
					$thumb = $this->config->item('image_thumb');
					$thumb['source_image'] = $image['full_path'];
					$thumb['new_image'] = $this->path_thumbs;
				
					$this->image_lib->initialize($thumb);
					$this->image_lib->resize();
				}
				//END:UPLOAD IMAGE
				if($image)
					$model['image'] = $this->folder.$image['file_name'];
				$model['created'] = strtotime(date('Y-m-d'));
				$id_insert = $this->categories_m->update($id,$model);
				if($type == 'save')
					redirect('admin/'.$this->namespace.'/'.$this->section);
				if($type == 'add')
					redirect('admin/'.$this->namespace.'/'.$this->section.'/create');
				if($type == 'edit')
					redirect('admin/'.$this->namespace.'/'.$this->section.'/edit/'.$id);		
			}
		}
		//categories 
		$categories_radio = $this->categories_m->list_categories_radio(true,$name= 'parent_id',0,$remove = $id, $parent = $model['parent_id']);//de quy

	 	
		$this->template
			->set('model',$model)
			->append_css('error.css')
			->set('action',$this->action)
			->set('categories_radio',$categories_radio)
			->set_breadcrumb(lang('shop:categories'), 'admin/shop/categories')
			->set_breadcrumb(lang('shop:cat:edit'))
			->build($this->controller.'/form');
	}
}